﻿using BaseData.API.Read.Applications.DoctorScheduling.Command;
using BaseData.API.Read.Dtos.DoctorSchedulingDto;
using BaseData.Domain.DoctorScheduling;
using BaseData.Domain.HospitalInformation;
using BaseData.ErrorCode;
using BaseData.Infrastructure.Interfaces;
using MediatR;

namespace BaseData.API.Read.Applications.DoctorScheduling.CommandHandler
{
    public class GetDoctorSchedulingCommandHandler : IRequestHandler<GetDoctorSchedulingCommand, ApiResult<DoctorSchedulingDto>>
    {
        private readonly IBaseRepository<Scheduling> schbaseRepository;
        private readonly IBaseRepository<Departments> deptbaseRepository;
        private readonly IBaseRepository<Doctors> doctbaseRepository;

        public GetDoctorSchedulingCommandHandler(IBaseRepository<Departments> deptbaseRepository, IBaseRepository<Scheduling> schbaseRepository, IBaseRepository<Doctors> doctbaseRepository)
        {
            this.schbaseRepository = schbaseRepository;
            this.deptbaseRepository = deptbaseRepository;
            this.doctbaseRepository = doctbaseRepository;
        }
        public Task<ApiResult<DoctorSchedulingDto>> Handle(GetDoctorSchedulingCommand request, CancellationToken cancellationToken)
        {
            ApiResult<DoctorSchedulingDto> result = new ApiResult<DoctorSchedulingDto>();
            result.Code = ApiEnum.查询成功;
            result.Message = ApiEnum.查询成功.ToString();
            var list = from a in schbaseRepository.GetAll()
                       join b in deptbaseRepository.GetAll()
                       on a.DepetmentId equals b.DepartmentId
                       join c in doctbaseRepository.GetAll()
                       on a.DoctorId equals c.DoctorId
                       select new DoctorSchedulingDto
                       {
                           SchedulingId = a.SchedulingId,
                           DoctorName = c.DoctorName,
                           DoctorId = c.DoctorId,
                           StartDate = a.StartDate,
                           Session = a.Session,
                           ConsultationTime = a.ConsultationTime,
                           DepetmentId = b.DepartmentId,
                           DepartmentName = b.DepartmentName,
                           Num = a.Num,
                           PatientNum = a.PatientNum,
                           SchedulingState = a.SchedulingState,
                       };
            var entity=list.FirstOrDefault(x=>x.SchedulingId==request.schedulingId);
            result.Data = entity;
            return Task.FromResult(result);

        }
    }
}
